package com.xydtech.ecard.merchant.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xydtech.common.core.domain.entity.SysDept;
import com.xydtech.ecard.merchant.domain.*;
import com.xydtech.ecard.merchant.mapper.EcardBranchAgricultureChartMapper;
import com.xydtech.ecard.merchant.service.IEcardBranchAgricultureChartService;
import com.xydtech.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 电子名片新型农业主体信息图表统计Service业务层处理
 *
 * @author yuzhiguo
 * @date 2024-09-12 上午10:49
 */
@Service
public class EcardBranchAgricultureChartServiceImpl implements IEcardBranchAgricultureChartService {

    @Autowired
    private EcardBranchAgricultureChartMapper ecardBranchAgricultureChartMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 本人农业建档拜访数量
     * @param userId
     * @return
     */
    @Override
    public JSONObject getAgrFilingVisitsStatistics(Long userId) {
        JSONObject statisticsData = new JSONObject();
        List<EcardBranchEcardChart> perFilingVisitsNums = ecardBranchAgricultureChartMapper.selectAgrFilingVisitsByUserId(userId);
        statisticsData.put("agrPerFilingNums",perFilingVisitsNums.get(0).getMerchantNum());//农业本人累计建档总数
        statisticsData.put("agrPerVisitNums",perFilingVisitsNums.get(1).getMerchantNum());//农业本人累计拜访总数
        statisticsData.put("agrPerDoVisitNums",perFilingVisitsNums.get(2).getMerchantNum());//农业本人超时拜访总数
        statisticsData.put("agrPerUnDoVisitNums",perFilingVisitsNums.get(3).getMerchantNum());//农业本人超时未拜访总数
        statisticsData.put("agrPerFilingThisMonthNums",perFilingVisitsNums.get(4).getMerchantNum());//农业本人本月新增建档总数
        statisticsData.put("agrPerFilingCompareNums",perFilingVisitsNums.get(4).getMerchantNum() - perFilingVisitsNums.get(5).getMerchantNum());//农业本人建档较上月对比
        if (statisticsData.getLong("agrPerFilingCompareNums") >= 0){
            statisticsData.put("agrPerFilingType", "1");//较上月农业本人建档类型（1：上升，2：下降）
        } else {
            statisticsData.put("agrPerFilingType", "2");//较上月农业本人建档类型（1：上升，2：下降）
        }
        statisticsData.put("agrPerVisitThisMonthNums",perFilingVisitsNums.get(6).getMerchantNum());//农业本人本月新增拜访总数
        statisticsData.put("agrPerVisitCompareNums",perFilingVisitsNums.get(6).getMerchantNum() - perFilingVisitsNums.get(7).getMerchantNum());//农业本人拜访较上月对比
        if (statisticsData.getLong("agrPerVisitCompareNums") >= 0){
            statisticsData.put("agrPerVisitType", "1");//较上月农业本人拜访类型（1：上升，2：下降）
        } else {
            statisticsData.put("agrPerVisitType", "2");//较上月农业本人拜访类型（1：上升，2：下降）
        }
        return statisticsData;
    }

    /**
     * 全行农业建档拜访数量
     * @param deptId
     * @return
     */
    @Override
    public JSONObject getAgrAllFilingVisitsNums(Long deptId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        JSONObject statisticsData = new JSONObject();
        List<EcardBranchEcardChart> allFilingVisitsNums = ecardBranchAgricultureChartMapper.getAgrAllFilingVisitsNums(sysDept.getDeptId());
        statisticsData.put("agrFilingNums",allFilingVisitsNums.get(0).getMerchantNum());//全行农业建档总数
        statisticsData.put("agrFilingLastMonthNums",allFilingVisitsNums.get(1).getMerchantNum());//上月新增农业建档总数
        statisticsData.put("agrFilingThisMonthNums",allFilingVisitsNums.get(2).getMerchantNum());//本月新增农业建档总数
        statisticsData.put("agrFilingCompareNums",allFilingVisitsNums.get(2).getMerchantNum() - allFilingVisitsNums.get(1).getMerchantNum());//农业建档较上月对比
        statisticsData.put("agrVisitNums",allFilingVisitsNums.get(3).getMerchantNum());//农业拜访总次数
        statisticsData.put("agrVisitLastMonthNums",allFilingVisitsNums.get(4).getMerchantNum());//上月新增农业拜访总数
        statisticsData.put("agrVisitThisMonthNums",allFilingVisitsNums.get(5).getMerchantNum());//本月新增农业拜访总数
        statisticsData.put("agrVisitCompareNums",allFilingVisitsNums.get(5).getMerchantNum() - allFilingVisitsNums.get(4).getMerchantNum());//农业拜访较上月对比
        return statisticsData;
    }

    /**
     * 农业超时拜访数量
     * @param deptId
     * @return
     */
    @Override
    public JSONObject getAgrTimeoutVisitsNums(Long deptId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        JSONObject statisticsData = new JSONObject();
        List<EcardBranchEcardChart> allTimeoutVisitsNums = ecardBranchAgricultureChartMapper.getAgrTimeoutVisitsNums(sysDept.getDeptId());
        statisticsData.put("agrDoTaskNums",allTimeoutVisitsNums.get(0).getDoTaskSum());//农业超时拜访总数
        statisticsData.put("agrDoTaskLastMonthNums",allTimeoutVisitsNums.get(1).getDoTaskSum());//上月新增农业超时拜访总数
        statisticsData.put("agrDoTaskThisMonthNums",allTimeoutVisitsNums.get(2).getDoTaskSum());//本月新增农业超时拜访总数
        statisticsData.put("agrDoTaskCompareNums",allTimeoutVisitsNums.get(2).getDoTaskSum() - allTimeoutVisitsNums.get(1).getDoTaskSum());//农业超时拜访较上月对比
        statisticsData.put("agrUndoTaskNums",allTimeoutVisitsNums.get(3).getDoTaskSum());//农业超时未拜访总次数
        statisticsData.put("agrUndoTaskLastMonthNums",allTimeoutVisitsNums.get(4).getDoTaskSum());//上月新增农业超时未拜访总数
        statisticsData.put("agrUndoTaskThisMonthNums",allTimeoutVisitsNums.get(5).getDoTaskSum());//本月新增农业超时未拜访总数
        statisticsData.put("agrUndoTaskCompareNums",allTimeoutVisitsNums.get(5).getDoTaskSum() - allTimeoutVisitsNums.get(4).getDoTaskSum());//农业超时未拜访较上月对比
        return statisticsData;
    }

    /**
     * 查询农业信息建档折线图数据
     * @param deptId
     * @return
     */
    @Override
    public List<EcardBranchEcardChart> getAgrFilingLineChart(Long deptId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        return ecardBranchAgricultureChartMapper.getAgrFilingLineChart(sysDept.getDeptId());
    }

    /**
     * 查询农业信息建档折线图数据（小程序接口）
     * @param deptId
     * @return
     */
    @Override
    public List<EcardBranchEcardChart> getAgrFilingLineChartByApplet(Long deptId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        return ecardBranchAgricultureChartMapper.getAgrFilingLineChartByApplet(sysDept.getDeptId());
    }

    /**
     * 查询农业信息拜访折线图数据
     * @param deptId
     * @return
     */
    @Override
    public List<EcardBranchEcardChart> getAgrVisitLineChart(Long deptId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        return ecardBranchAgricultureChartMapper.getAgrVisitLineChart(sysDept.getDeptId());
    }

    /**
     * 查询农业信息拜访柱状图数据接口（小程序接口）
     * @param deptId
     * @return
     */
    @Override
    public List<EcardBranchEcardChart> getAgrVisitLineChartByApplet(Long deptId) {
        SysDept sysDept = sysDeptService.selectSecondLevelDeptByDeptId(deptId);
        return ecardBranchAgricultureChartMapper.getAgrVisitLineChartByApplet(sysDept.getDeptId());
    }

    /**
     * 机构农业建档排名
     * @param ecardBranchDeptFiling
     * @return
     */
    @Override
    public List<EcardBranchDeptFiling> getAgrDeptFilingList(EcardBranchDeptFiling ecardBranchDeptFiling) {
        return ecardBranchAgricultureChartMapper.getAgrDeptFilingList(ecardBranchDeptFiling);
    }

    /**
     * 机构农业拜访次数排名
     * @param ecardBranchDeptVisit
     * @return
     */
    @Override
    public List<EcardBranchDeptVisit> getAgrDeptVisitsList(EcardBranchDeptVisit ecardBranchDeptVisit) {
        return ecardBranchAgricultureChartMapper.getAgrDeptVisitsList(ecardBranchDeptVisit);
    }

    /**
     * 机构农业超时拜访次数排名
     * @param ecardBranchDeptDoTask
     * @return
     */
    @Override
    public List<EcardBranchDeptDoTask> getAgrDeptDoTaskList(EcardBranchDeptDoTask ecardBranchDeptDoTask) {
        return ecardBranchAgricultureChartMapper.getAgrDeptDoTaskList(ecardBranchDeptDoTask);
    }

    /**
     * 机构农业超时未拜访次数排名
     * @param ecardBranchDeptUndoTask
     * @return
     */
    @Override
    public List<EcardBranchDeptUndoTask> getAgrDeptUnDoTaskList(EcardBranchDeptUndoTask ecardBranchDeptUndoTask) {
        return ecardBranchAgricultureChartMapper.getAgrDeptUnDoTaskList(ecardBranchDeptUndoTask);
    }

    /**
     * 营销人员农业建档排名
     * @param ecardBranchUserFiling
     * @return
     */
    @Override
    public List<EcardBranchUserFiling> getAgrUserFilingList(EcardBranchUserFiling ecardBranchUserFiling) {
        return ecardBranchAgricultureChartMapper.getAgrUserFilingList(ecardBranchUserFiling);
    }

    /**
     * 营销人员农业拜访次数排名
     * @param ecardBranchUserVisit
     * @return
     */
    @Override
    public List<EcardBranchUserVisit> getAgrUserVisitsList(EcardBranchUserVisit ecardBranchUserVisit) {
        return ecardBranchAgricultureChartMapper.getAgrUserVisitsList(ecardBranchUserVisit);
    }

    /**
     * 营销人员农业超时拜访次数排名
     * @param ecardBranchUserDoTask
     * @return
     */
    @Override
    public List<EcardBranchUserDoTask> getAgrUserDoTaskList(EcardBranchUserDoTask ecardBranchUserDoTask) {
        return ecardBranchAgricultureChartMapper.getAgrUserDoTaskList(ecardBranchUserDoTask);
    }

    /**
     * 营销人员农业超时未拜访次数排名
     * @param ecardBranchUserUndoTask
     * @return
     */
    @Override
    public List<EcardBranchUserUndoTask> getAgrUserUnDoTaskList(EcardBranchUserUndoTask ecardBranchUserUndoTask) {
        return ecardBranchAgricultureChartMapper.getAgrUserUnDoTaskList(ecardBranchUserUndoTask);
    }
}
